package com.graduationdesign.zengyan.graduationdesignapp.ui.presenter;

import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.profileRepository.MainService;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.BaseLoadingView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.BasePresenter;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.Transforms;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.errorhandle.BaseErrorView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.contact.OrgContact;

/**
 * Created by ZengYan on 2018/4/22.
 *
 * @desc :
 */
public class OrgsPresenter extends BasePresenter implements OrgContact.Presenter {


    private OrgContact.ShowOrgPeoples mShowOrgPeoples;
    private OrgContact.ShowOrgEvent showOrgEvent;
    private OrgContact.ShowOrgInfo showOrgInfo;
    private MainService mMainService;


    private OrgsPresenter(Builder builder) {
        super(builder.mBaseErrorView);
        mShowOrgPeoples = builder.mShowOrgPeoples;
        showOrgEvent = builder.showOrgEvent;
        showOrgInfo = builder.showOrgInfo;
        mMainService = builder.mMainService;
    }


    public static Builder newBuilder() {
        return new Builder();
    }


    @Override
    protected String provideTag() {
        return "orgPresenter";
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unSubsrible() {
        compositeDisposable.clear();
    }


    @Override
    public void loadAllOrgs(BaseLoadingView loadingView) {
    }

    @Override
    public void loadOrg(int id, BaseLoadingView loadingView) {
        mMainService.getOrgById(id)
                .compose(Transforms.applyAll(loadingView, compositeDisposable))
                .subscribe(organizationResult -> {
                    if ( showOrgInfo == null) {
                        return;
                    }
                    showOrgInfo.showOrg(organizationResult.getData());
                },mErrorConsumer);
    }

    @Override
    public void loadPeopleInOrg(int id, BaseLoadingView loadingView) {
        mMainService.getPeoplesInOrg(id)
                .compose(Transforms.applyAll(loadingView, compositeDisposable))
                .map(peoplesModelResult -> peoplesModelResult.getData().getUsers())
                .subscribe(users -> {
                    if ( mShowOrgPeoples == null) {
                        return;
                    }
                    mShowOrgPeoples.showOrgPeoples(users);
                },mErrorConsumer);
    }


    public static final class Builder {
        private OrgContact.ShowOrgPeoples mShowOrgPeoples;
        private OrgContact.ShowOrgEvent showOrgEvent;
        private OrgContact.ShowOrgInfo showOrgInfo;
        private MainService mMainService;
        private BaseErrorView mBaseErrorView;

        private Builder() {
        }

        public Builder setmBaseErrorView(BaseErrorView mBaseErrorView) {
            this.mBaseErrorView = mBaseErrorView;
            return this;
        }

        public Builder setMShowOrgPeoples(OrgContact.ShowOrgPeoples val) {
            mShowOrgPeoples = val;
            return this;
        }

        public Builder setShowOrgEvent(OrgContact.ShowOrgEvent val) {
            showOrgEvent = val;
            return this;
        }

        public Builder setShowOrgInfo(OrgContact.ShowOrgInfo val) {
            showOrgInfo = val;
            return this;
        }

        public Builder setMMainService(MainService val) {
            mMainService = val;
            return this;
        }

        public OrgsPresenter build() {
            return new OrgsPresenter(this);
        }
    }
}
